home *** CD-ROM | disk | FTP | other *** search
/ Univers Mac Interactif 42 / Univers Mac Interactif - Issue 42.iso / Internet / MacHTTP 2.0 / MacHTTP Software & Docs / Tutorials / Extending MacHTTP Scripts / Email.txt < prev    next >
Text File  |  1994-12-16  |  10KB  |  218 lines

  1. -- set these properties outside of the event.  That way they will only be set 
  2. -- once each time the app is run, not once for each event.
  3. property crlf : (ASCII character 13) & (ASCII character 10)
  4. -- This is a standard header for HTML files.
  5. property http_10_header : "HTTP/1.0 200 OK" & crlf & "Server: MacHTTP" & crlf & ¬
  6.     "MIME-Version: 1.0" & crlf & "Content-type: text/html" & crlf & crlf
  7. -- Idletime is how long you want it to remain open to 
  8. -- wait for another event. Idletime is in seconds.
  9. -- Datestamp will contain the current date.  Initialize it here.
  10. property idletime : 300 -- set to 5 minutes
  11. property datestamp : 0
  12.  
  13. -- this bit of code outside the sdoc event is executed at launch-time
  14. -- it is neccesary because an idle event can happen between
  15. -- launch and the receipt of an sdoc event (and in fact often does)
  16. set datestamp to current date
  17.  
  18. -- This is the loop for AppleEvents received by the application.
  19. -- When you check the syntax, AppleScript will ask you to locate 
  20. -- MacHTTP and will set the correct name at that time.
  21. on «event WWWΩsdoc» path_args ¬
  22.     given «class kfor»:http_search_args, «class post»:post_args, «class meth»:method, «class addr»:client_address, «class user»:username, «class pass»:password, «class frmu»:from_user, «class svnm»:server_name, «class svpt»:server_port, «class scnm»:script_name, «class ctyp»:content_type
  23.     -- Variables available for use:
  24.     -- http_search_args - stuff in the URL after a ?
  25.     -- post_args - stuff in the URL after a $
  26.     -- method - GET, POST, etc. Used to tell if post_args are valid
  27.     -- client_address - IP address or domain name of remote client's host
  28.     -- from_user - non-standard. e-mail address of remote user
  29.     -- username - authenticated user name
  30.     -- password - authenticated password
  31.     -- server_name - name or IP address of this server
  32.     -- server_port - TCP/IP port number being used by this server
  33.     -- script_name - URL name of this script
  34.     -- content_type - MIME content type of post_args
  35.     
  36.     -- Using the "try" clause causes the "on error" routine to be run
  37.     -- if an error occurs instead of crashing.
  38.     -- If the error occurs outside the "try"/"end try" space then
  39.     -- the "on error" routine is NOT run.
  40.     try
  41.         -- save the current date and time to check later for quitting
  42.         set datestamp to current date
  43.         
  44.         -- Parse the post_args data into variables for processing
  45.         -- this script only handles the post_args.  Handling the other 
  46.         -- variables is trivial compared to this.
  47.         set return_page to http_10_header ¬
  48.             & ¬
  49.             "<HTML><HEAD><TITLE>Email Form Results</TITLE></HEAD>" & "<BODY><H1>Email Form Results</H1>" & return
  50.         
  51.         -- this will produce a list of "name=value" pairs 
  52.         set postarglist to tokenize post_args with delimiters {"&"}
  53.         
  54.         -- process each list pair in postarglist
  55.         -- store the original AppleScript text item delimiters
  56.         set oldDelim to AppleScript's text item delimiters
  57.         -- use "=" to delimit the pairs
  58.         set AppleScript's text item delimiters to {"="}
  59.         -- traverse the list and process the items
  60.         repeat with currpostarg in postarglist
  61.             set currname to first text item of currpostarg
  62.             if currname = "name" then
  63.                 set username to (Decode URL (dePlus (last text item of currpostarg)))
  64.             else if currname = "address" then
  65.                 set from_address to (Decode URL (dePlus (last text item of currpostarg)))
  66.             else if currname = "subject" then
  67.                 set email_subject to (Decode URL (dePlus (last text item of currpostarg)))
  68.             else if currname = "message" then
  69.                 set email_body to (Decode URL (dePlus (last text item of currpostarg)))
  70.             else if currname = "S" then
  71.                 -- ignore it.  That's the Submit button.
  72.             else
  73.                 -- you have a variable who's name you don't know.  Bad news!
  74.                 -- create your own errMsg and errNum to pass back
  75.                 -- the number 100 has no significance.  I just chose it at random
  76.                 error ("Unknown field in post_args: " & currname) number 100
  77.             end if
  78.         end repeat
  79.         -- restore the old AppleScript text item delimiters settings
  80.         set AppleScript's text item delimiters to oldDelim
  81.         
  82.         -- NOTE: Set the "to_address" to be the address to which you want all of the mail sent.
  83.         set to_address to "jonwd@tjp.washington.edu"
  84.         set return_page to return_page ¬
  85.             & "E-mail has been sent to " & to_address & return ¬
  86.             & "<P><I>Message generated at: " & (current date) ¬
  87.             & "</I>" & "</BODY></HTML>"
  88.         set email_host to "homer.u.washington.edu"
  89.         send_message(email_host, to_address, from_address, email_subject, email_body)
  90.         -- return the page created.  A return statement ends the 
  91.         -- processing of the AppleEvent
  92.         return return_page
  93.         
  94.         -- here is the routine to run if an error occurs
  95.         -- errMsg contains the message sent by the System
  96.         -- errNum contains the number of the error (negative for System, AE, or AS errors)
  97.     on error errMsg number errNum
  98.         -- create a page of HTML text to return
  99.         set return_page to http_10_header ¬
  100.             & ¬
  101.             "<HTML><HEAD><TITLE>Error Page</TITLE></HEAD>" & "<BODY><H1>Error Encountered!</H1>" & return ¬
  102.             & "An error was encountered while trying to run this script." & return
  103.         set return_page to return_page ¬
  104.             & "<H3>Error Message</H3>" & return & errMsg & return ¬
  105.             & "<H3>Error Number</H3>" & return & errNum & return ¬
  106.             & "<H3>Date</H3>" & return & (current date) & return
  107.         set return_page to return_page ¬
  108.             & ¬
  109.             "<HR>Please notify Jon Wiederspan at " & ¬
  110.             "<A HREF=\"mailto:jonwd@tjp.washington.edu\">jonwd@tjp.washington.edu</A>" & " of this error." & "</BODY></HTML>"
  111.         -- return the page created.  A return statement ends the 
  112.         -- processing of the AppleEvent
  113.         return return_page
  114.     end try
  115. end «event WWWΩsdoc»
  116.  
  117. -- This handler uses the TCPIP Scripting Suite to send e-mail to your mailhost.
  118. -- Note that email_host is the name of a mail server, not just your domain name.
  119. -- For some reason that I haven't figured out yet (from not reading the docs), 
  120. -- this requires that you send a receipt as well as a message.
  121. on send_message(email_host, to_address, from_address, email_subject, email_body)
  122.     set crlf to (ASCII character 13) & (ASCII character 10)
  123.     set sss to (tcp connect to host email_host port 25)
  124.     try
  125.         readresponse(sss)
  126.         tcp write data "mail from: " & from_address & return ¬
  127.             stream sss using ISO88591
  128.         readresponse(sss)
  129.         tcp write data "rcpt to: " & to_address & return stream sss using ISO88591
  130.         readresponse(sss)
  131.         tcp write data "data" & return stream sss using ISO88591
  132.         readresponse(sss)
  133.         tcp write data "To: " & to_address & return stream sss using ISO88591
  134.         tcp write data "Subject: " & email_subject & return stream sss using ISO88591
  135.         tcp write data email_body & crlf & return stream sss using ISO88591
  136.         tcp write data "." & return stream sss using ISO88591
  137.         readresponse(sss)
  138.         tcp close stream sss
  139.         return
  140.     on error errMsg number errNum
  141.         set return_page to http_10_header ¬
  142.             & ¬
  143.             "<HTML><HEAD><TITLE>Error Page</TITLE></HEAD>" & "<BODY><H1>Error Encountered!</H1>" & return ¬
  144.             & "An error was encountered while trying to send the mail." & return
  145.         set return_page to return_page ¬
  146.             & "<H3>Error Message</H3>" & return & errMsg & return ¬
  147.             & "<H3>Error Number</H3>" & return & errNum & return ¬
  148.             & "<H3>Date</H3>" & return & (current date) & return
  149.         set return_page to return_page ¬
  150.             & ¬
  151.             "<HR>Please notify Jon Wiederspan at " & ¬
  152.             "<A HREF=\"mailto:jonwd@tjp.washington.edu\">jonwd@tjp.washington.edu</A>" & " of this error." & "</BODY></HTML>"
  153.         return return_page
  154.     end try
  155. end send_message
  156.  
  157. -- This handler is used to read the information returned from the mail server.
  158. -- It requires the TCPIP Scripting Suite.
  159. on readresponse(sstream)
  160.     set LF to ASCII character (10)
  161.     set continuechar to "-"
  162.     set wholemessage to ""
  163.     try
  164.         repeat until continuechar = " "
  165.             repeat until (tcp ahead characters LF stream sstream)
  166.             end repeat
  167.             set readline to (tcp read until characters LF stream sstream using ISO88591)
  168.             set scan to (scanline(readline))
  169.             set continuechar to item 2 of scan
  170.             set wholemessage to wholemessage & " " & item 3 of scan
  171.         end repeat
  172.         set errorCode to item 1 of scan as integer
  173.         if (errorCode ≥ 400) then
  174.             error wholemessage number errorCode
  175.         end if
  176.     on error errMsg number errNum
  177.         set return_page to http_10_header ¬
  178.             & ¬
  179.             "<HTML><HEAD><TITLE>Error Page</TITLE></HEAD>" & "<BODY><H1>Error Encountered!</H1>" & return ¬
  180.             & "An error was encountered while trying to read the mailer response." & return
  181.         set return_page to return_page ¬
  182.             & "<H3>Error Message</H3>" & return & errMsg & return ¬
  183.             & "<H3>Error Number</H3>" & return & errNum & return ¬
  184.             & "<H3>Date</H3>" & return & (current date) & return
  185.         set return_page to return_page ¬
  186.             & ¬
  187.             "<HR>Please notify Jon Wiederspan at " & ¬
  188.             "<A HREF=\"mailto:jonwd@tjp.washington.edu\">jonwd@tjp.washington.edu</A>" & " of this error." & "</BODY></HTML>"
  189.         return return_page
  190.     end try
  191. end readresponse
  192.  
  193. -- This handler is used to test for matching messages.
  194. -- Matches lines like: 250 test... Sender ok
  195. on scanline(lline)
  196.     return {characters 1 through 3 of lline as string, character 4 ¬
  197.         of lline as string, characters 5 through end of lline as string}
  198. end scanline
  199.  
  200. -- The idle function is run everytime the system sends an idle message.
  201. -- If the current date is more than idletime seconds more than the last date 
  202. -- then it is time to quit.
  203. -- The return value tells the system how long to wait before
  204. on idle
  205.     if (current date) > (datestamp + idletime) then
  206.         quit
  207.     end if
  208.     return 5
  209. end idle
  210.  
  211. -- This code allows you to do any clean-up that might be necessary.
  212. -- Example: you could write the quit event time to a log to see 
  213. -- how often the applet is running.
  214. on quit
  215.     -- do any clean-up chores here
  216.     continue quit
  217. end quit
  218.